package com.xiaohao.news.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaohao.news.common.R;
import com.xiaohao.news.entity.Type;
import com.xiaohao.news.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @PutMapping("/add")
    public R<String> addType(@RequestBody Type type) {

        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Type::getName, type.getName());
        Type one = typeService.getOne(wrapper);
        log.info("查询结果:{}", one);
        if (one != null) {
            return R.error("当前分类已存在");
        }
        typeService.save(type);
        return R.success("添加成功");
    }


    /**
     * 返回新闻分类列表
     * @return
     */
    @GetMapping("/list")
    public R<List<Type>> list() {
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Type::getId);
        List<Type> list = typeService.list(wrapper);
        list.toString();
        return R.success(list);
    }
}
